<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="OPENID" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入OPENID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="phoneNum">
        <el-input
          v-model="queryParams.phoneNum"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户等级" prop="lv">
        <el-input
          v-model="queryParams.lv"
          placeholder="请输入用户等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户经验值 " prop="exp">
        <el-input
          v-model="queryParams.exp"
          placeholder="请输入用户经验值 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体力值" prop="energys">
        <el-input
          v-model="queryParams.energys"
          placeholder="请输入体力值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前人物id" prop="roleId">
        <el-input
          v-model="queryParams.roleId"
          placeholder="请输入当前人物id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前飞行器id" prop="aircraftId">
        <el-input
          v-model="queryParams.aircraftId"
          placeholder="请输入当前飞行器id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户灵石数量" prop="diamond">
        <el-input
          v-model="queryParams.diamond"
          placeholder="请输入用户灵石数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户极品灵石数量" prop="premiumDiamond">
        <el-input
          v-model="queryParams.premiumDiamond"
          placeholder="请输入用户极品灵石数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通用道具数量" prop="universalProps">
        <el-input
          v-model="queryParams.universalProps"
          placeholder="请输入通用道具数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高分" prop="highestScore">
        <el-input
          v-model="queryParams.highestScore"
          placeholder="请输入最高分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="0 不封禁 1 封禁" prop="ban">
        <el-select v-model="queryParams.ban" placeholder="请选择0 不封禁 1 封禁" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="补签道具" prop="signInPrpos">
        <el-input
          v-model="queryParams.signInPrpos"
          placeholder="请输入补签道具"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="普通宝箱数量" prop="boxes">
        <el-input
          v-model="queryParams.boxes"
          placeholder="请输入普通宝箱数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="超级宝箱数量" prop="superBoxes">
        <el-input
          v-model="queryParams.superBoxes"
          placeholder="请输入超级宝箱数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最近登录时间" prop="recentLoginTime">
        <el-date-picker clearable
          v-model="queryParams.recentLoginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最近登录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="注册时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择注册时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="userId" />
      <el-table-column label="OPENID" align="center" prop="openId" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="电话号码" align="center" prop="phoneNum" />
      <el-table-column label="用户头像" align="center" prop="avatarUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatarUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="用户等级" align="center" prop="lv" />
      <el-table-column label="用户经验值 " align="center" prop="exp" />
      <el-table-column label="体力值" align="center" prop="energys" />
      <el-table-column label="当前人物id" align="center" prop="roleId" />
      <el-table-column label="当前飞行器id" align="center" prop="aircraftId" />
      <el-table-column label="用户灵石数量" align="center" prop="diamond" />
      <el-table-column label="用户极品灵石数量" align="center" prop="premiumDiamond" />
      <el-table-column label="通用道具数量" align="center" prop="universalProps" />
      <el-table-column label="最高分" align="center" prop="highestScore" />
      <el-table-column label="0 不封禁 1 封禁" align="center" prop="ban">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.ban"/>
        </template>
      </el-table-column>
      <el-table-column label="补签道具" align="center" prop="signInPrpos" />
      <el-table-column label="普通宝箱数量" align="center" prop="boxes" />
      <el-table-column label="超级宝箱数量" align="center" prop="superBoxes" />
      <el-table-column label="最近登录时间" align="center" prop="recentLoginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recentLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="OPENID" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入OPENID" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNum">
          <el-input v-model="form.phoneNum" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="用户头像" prop="avatarUrl">
          <image-upload v-model="form.avatarUrl"/>
        </el-form-item>
        <el-form-item label="用户等级" prop="lv">
          <el-input v-model="form.lv" placeholder="请输入用户等级" />
        </el-form-item>
        <el-form-item label="用户经验值 " prop="exp">
          <el-input v-model="form.exp" placeholder="请输入用户经验值 " />
        </el-form-item>
        <el-form-item label="体力值" prop="energys">
          <el-input v-model="form.energys" placeholder="请输入体力值" />
        </el-form-item>
        <el-form-item label="当前人物id" prop="roleId">
          <el-input v-model="form.roleId" placeholder="请输入当前人物id" />
        </el-form-item>
        <el-form-item label="当前飞行器id" prop="aircraftId">
          <el-input v-model="form.aircraftId" placeholder="请输入当前飞行器id" />
        </el-form-item>
        <el-form-item label="用户灵石数量" prop="diamond">
          <el-input v-model="form.diamond" placeholder="请输入用户灵石数量" />
        </el-form-item>
        <el-form-item label="用户极品灵石数量" prop="premiumDiamond">
          <el-input v-model="form.premiumDiamond" placeholder="请输入用户极品灵石数量" />
        </el-form-item>
        <el-form-item label="通用道具数量" prop="universalProps">
          <el-input v-model="form.universalProps" placeholder="请输入通用道具数量" />
        </el-form-item>
        <el-form-item label="最高分" prop="highestScore">
          <el-input v-model="form.highestScore" placeholder="请输入最高分" />
        </el-form-item>
        <el-form-item label="0 不封禁 1 封禁" prop="ban">
          <el-select v-model="form.ban" placeholder="请选择0 不封禁 1 封禁">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="补签道具" prop="signInPrpos">
          <el-input v-model="form.signInPrpos" placeholder="请输入补签道具" />
        </el-form-item>
        <el-form-item label="普通宝箱数量" prop="boxes">
          <el-input v-model="form.boxes" placeholder="请输入普通宝箱数量" />
        </el-form-item>
        <el-form-item label="超级宝箱数量" prop="superBoxes">
          <el-input v-model="form.superBoxes" placeholder="请输入超级宝箱数量" />
        </el-form-item>
        <el-form-item label="最近登录时间" prop="recentLoginTime">
          <el-date-picker clearable
            v-model="form.recentLoginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最近登录时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/system/users";

export default {
  name: "User",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openId: null,
        username: null,
        password: null,
        phoneNum: null,
        avatarUrl: null,
        lv: null,
        exp: null,
        energys: null,
        roleId: null,
        aircraftId: null,
        diamond: null,
        premiumDiamond: null,
        universalProps: null,
        highestScore: null,
        ban: null,
        signInPrpos: null,
        boxes: null,
        superBoxes: null,
        recentLoginTime: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        openId: null,
        username: null,
        password: null,
        phoneNum: null,
        avatarUrl: null,
        lv: null,
        exp: null,
        energys: null,
        roleId: null,
        aircraftId: null,
        diamond: null,
        premiumDiamond: null,
        universalProps: null,
        highestScore: null,
        ban: null,
        signInPrpos: null,
        boxes: null,
        superBoxes: null,
        recentLoginTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {...row};
      const userId = row.userId || this.ids
      getUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
        this.form = {...row};
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除用户信息编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
